package cn.gwm.flink.streaming.dws.reservecharge;

import cn.gwm.flink.streaming.constant.BaseFields;
import cn.gwm.flink.streaming.dwm.charge.model.ChargeModelEnum;
import cn.gwm.flink.streaming.dwm.charge.model.ChargeStateEnum;
import cn.gwm.flink.streaming.dwm.charge.model.ChargeTypeEnum;
import cn.gwm.flink.streaming.dws.reservecharge.beans.RcElectricConfigValleyPeakDao;
import cn.gwm.flink.streaming.dws.reservecharge.enums.ChargeAreaTypeEnum;
import cn.gwm.flink.streaming.dws.reservecharge.enums.ChargeBusiStatusEnum;
import cn.gwm.flink.streaming.dws.reservecharge.enums.ChargeConnectTypeEnum;
import cn.gwm.flink.streaming.dws.reservecharge.enums.ChargePowerTypeEnum;
import cn.gwm.utils.DateTimeUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * @author Liu.Nanfang
 * @date 2023/2/13 15:54
 * @description
 */
public class BuChargeHandle {
    /**
     * 根据充电主题的状态合并
     *
     * @param stateEnum
     * @return
     */
    public static Integer handleBuChargeStatus(ChargeStateEnum stateEnum) {
        String stateEnumResult;
        switch (stateEnum) {
            // 等待充电
            case WAIT:
            case DELAY:
                stateEnumResult = ChargeBusiStatusEnum.WAIT.getCode();
                break;
            // 充电预热
            case PREHEAT:
                stateEnumResult = ChargeBusiStatusEnum.PREHEAT.getCode();
                break;
            // 充电中
            case CHARGING:
                stateEnumResult = ChargeBusiStatusEnum.UNDERWAY.getCode();
                break;
            // 充电加热
            case CHARGING_HEATING:
                stateEnumResult = ChargeBusiStatusEnum.CHARGING_HEATING.getCode();
                break;
            // 充电结束
            case FULLY:
            case END:
                stateEnumResult = ChargeBusiStatusEnum.END.getCode();
                break;
            // 充电中断
            case UNABLE:
            case SUSPEND:
                stateEnumResult = ChargeBusiStatusEnum.FAULT.getCode();
                break;
            // 发生故障
            case FAULT:
            case LOGIC_BREAK:
            case ERROR:
            case UNKNOWN:
                // FAULT LOGIC_BREAK ERROR UNKNOWN
                stateEnumResult = ChargeBusiStatusEnum.SUSPEND.getCode();
                break;
            // 待识别
            default:
                stateEnumResult = ChargeBusiStatusEnum.OTHER.getCode();
                break;
        }
        return Integer.valueOf(stateEnumResult);
    }

    /**
     * 处理充电各种状态转换类型
     *
     * @param nowData
     * @param config
     */
    public static void chargeTypeHandle(ReserveChargeData nowData, ReserveChargeElectricConfig config) {
        chargePowerTypeHandle(nowData, config);
        chargeModelTypeHandle(nowData, config);
        chargeConnectTypeHandle(nowData);
        chargeTypeHandle(nowData);
        chargeAreaTypeHandle(nowData);

    }

    /**
     * 处理充电类型
     *
     * @param nowData
     */
    private static void chargeTypeHandle(ReserveChargeData nowData) {
        // 统一修改充电类型
        if (BaseFields.defaultVal.equals(nowData.getChargeType())) {
            nowData.setChargeType(ChargeTypeEnum.CHARGE_TYPE_OTHER.getCode());
        }
    }

    /**
     * 处理合并快慢充
     *
     * @param nowData
     */
    private static void chargeConnectTypeHandle(ReserveChargeData nowData) {
        if (Objects.nonNull(nowData.getOBC_ConnectSts_EV()) && nowData.getOBC_ConnectSts_EV() == 1) {
            nowData.setChargeConnectType(ChargeConnectTypeEnum.SLOW.getCode());
        } else {
            nowData.setChargeConnectType(ChargeConnectTypeEnum.QUICK.getCode());
        }
    }

    /**
     * 处理充电电量类型
     *
     * @param nowData
     */
    @SuppressWarnings("all")
    public static void chargePowerTypeHandle(ReserveChargeData nowData, ReserveChargeElectricConfig config) {
        String chargeType = nowData.getChargeType();
        String aoiType = nowData.getAoiType();
        // 民用充电
        if ((ChargeTypeEnum.CHARGE_TYPE_SLOW_CAR_GUN.getCode().equals(chargeType) ||
                ChargeTypeEnum.CHARGE_TYPE_SLOW.getCode().equals(chargeType))) {

            ChargePrice chargePrice = ChargePrice.getInstance();
            String useType = chargePrice.getFilterByAdCodeAndScope(nowData.getAdCode(), chargeType);

            if (StringUtils.isNotBlank(useType)) {
                nowData.setChargePowerType(ChargePowerTypeEnum.fromCode(useType).getCode());
            }

            // 根据当地用电类型判断是否为合表用电
//            if (ObjectUtil.isNotNull(config) && ChargeUseElectricTypeEnum.TABLE.getCode().equals(config.getUseElectricType())) {
//                nowData.setChargePowerType(ChargePowerTypeEnum.SLOW_MERGER.getCode());
//            } else {
//                nowData.setChargePowerType(ChargePowerTypeEnum.SLOW.getCode());
//            }
        } else if (ChargeTypeEnum.CHARGE_TYPE_QUICK.getCode().equals(chargeType) ||
                ChargeTypeEnum.CHARGE_TYPE_QUICK_SUPER.getCode().equals(chargeType) ||
                ChargeTypeEnum.CHARGE_TYPE_SLOW_BUSINESS.getCode().equals(chargeType)
        ) {
            nowData.setChargePowerType(ChargePowerTypeEnum.QUICK_SUPER.getCode());
        } else {
            nowData.setChargePowerType(ChargePowerTypeEnum.CHARGE_TYPE_OTHER.getCode());
        }


    }

    /**
     * 处理充电模式
     *
     * @param nowData
     * @param config
     */
    public static void chargeModelTypeHandle(ReserveChargeData nowData, ReserveChargeElectricConfig config) {
        if (ObjectUtil.isNull(config)) {
            return;
        }
        // 用户自定义充电 +  充电时间段为当地峰谷时段
        if (Integer.valueOf(ChargeModelEnum.CHARGE_APPOINTMENT.getCode()).equals(nowData.getChargeModel())) {
            if (config.getValleyPeak() == 1) {
                DateTime currentDate = DateUtil.date(nowData.getItem_time());
                for (ReserveChargeElectricConfig.ElectricConfig elConfig : config.getElectricConfigs()) {
                    for (RcElectricConfigValleyPeakDao peak : elConfig.getRcElectricConfigValleyPeakParamList()) {
                        // 判断充电完成情况下，是否完全在峰谷时间段
                        if (ChargeStateEnum.END.getCode().equals(nowData.getChargeStatus()) &&
                                ObjectUtil.isNotNull(nowData.getBusiStartChargeTime()) &&
                                ObjectUtil.isNotNull(nowData.getBusiEndChargeTime())) {

                            DateTime busiStartTime = DateUtil.date(nowData.getBusiStartChargeTime());
                            DateTime busiEndTime = DateUtil.date(nowData.getBusiEndChargeTime());
                            boolean startResult = DateTimeUtil.hourMinuteBetween(DateUtil.format(busiStartTime, "HH:mm"), peak.getStartTime(), peak.getEndTime());
                            boolean endResult = DateTimeUtil.hourMinuteBetween(DateUtil.format(busiEndTime, "HH:mm"), peak.getStartTime(), peak.getEndTime());
                            if (startResult && endResult && peak.getValleyPeakType() == 2) {
                                nowData.setChargeModel(Integer.valueOf(ChargeModelEnum.CHARGE_SMART_PEAK_VALLEY.getCode()));
                                return;
                            }

                        } else {
                            // 过程中依旧判断数据 itemTime
                            boolean result = DateTimeUtil.hourMinuteBetween(DateUtil.format(currentDate, "HH:mm"), peak.getStartTime(), peak.getEndTime());
                            // 判断在低谷时间段且为低谷类型
                            if (result && peak.getValleyPeakType() == 2) {
                                nowData.setChargeModel(Integer.valueOf(ChargeModelEnum.CHARGE_SMART_PEAK_VALLEY.getCode()));
                                return;
                            }

                        }

                    }

                }

            }
        }
    }

    /**
     * 处理充电地点类型
     * 1203 开头为居民小区
     * 0111 开头为充电站
     * 02 4S店
     *
     * @param nowData
     */
    @SuppressWarnings("all")
    public static void chargeAreaTypeHandle(ReserveChargeData nowData) {
        String aoiType = nowData.getAoiType();
        String chargeType = nowData.getChargeType();

        if (StringUtils.isBlank(aoiType) || BaseFields.defaultVal.equals(aoiType)) {
            nowData.setChargeAreaType(ChargeAreaTypeEnum.CHARGE_TYPE_UN_KNOWN.getCode());
            return;
        }

        if (aoiType.startsWith("1203")) {
            nowData.setChargeAreaType(ChargeAreaTypeEnum.CALL.getCode());

        } else if (aoiType.startsWith("0111")) {
            nowData.setChargeAreaType(ChargeAreaTypeEnum.CHARGE.getCode());

        } else if (aoiType.startsWith("02")) {
            nowData.setChargeAreaType(ChargeAreaTypeEnum.FOUR_S.getCode());
        } else if
        ((ChargeTypeEnum.CHARGE_TYPE_SLOW_CAR_GUN.getCode().equals(chargeType) || ChargeTypeEnum.CHARGE_TYPE_SLOW.getCode().equals(chargeType)) &&
                        (ChargePowerTypeEnum.SLOW.getCode().equals(nowData.getChargePowerType()) || ChargePowerTypeEnum.SLOW_MERGER.getCode().equals(nowData.getChargePowerType()))
        ) {
            // 使用用电类型与充电类型判断
            nowData.setChargeAreaType(ChargeAreaTypeEnum.CALL.getCode());
        } else {
            nowData.setChargeAreaType(ChargeAreaTypeEnum.CHARGE_TYPE_UN_KNOWN.getCode());
        }
    }
}
